package com.lib.controller.customer;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lib.model.enums.CustomerStatusEnum;
import com.lib.service.WxCustomerService;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by duqingxiang on 17/5/18.
 */
@RestController
@RequestMapping("/customer/menu")
public class CustomerMenuController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    WxCustomerService wxService;

    @RequestMapping("/createDefaultMenu")
    public String createDefaultMenu(){

        try {
            JSONObject json = new JSONObject();

            JSONArray buttons = new JSONArray();
            JSONObject btn1 = new  JSONObject();
            btn1.put("type","click");
            btn1.put("name","我的申请");
            String keyStr = "{\"type\":\""+WxConsts.XML_MSG_TEXT+"\",\"content\":\"APPLYING\"}";
            btn1.put("key",keyStr);
            buttons.add(btn1);


            JSONObject btn2 = new JSONObject();
            btn2.put("name","客户服务");

            JSONArray subBtns = new JSONArray();
            JSONObject sub1 = new JSONObject();
            sub1.put("name","关于我们");
            sub1.put("type","view");
            sub1.put("url","http://www.baidu.com");
            subBtns.add(sub1);

            JSONObject sub2 = new JSONObject();
            sub2.put("name","风险提示");
            sub2.put("type","view");
            sub2.put("url","http://www.baidu.com");
            subBtns.add(sub2);

            JSONObject sub3 = new JSONObject();
            sub3.put("name","逾期说明");
            sub3.put("type","view");
            sub3.put("url","http://www.baidu.com");
            subBtns.add(sub3);

            JSONObject sub4 = new JSONObject();
            sub4.put("name","如何还款");
            sub4.put("type","view");
            sub4.put("url","http://www.baidu.com");
            subBtns.add(sub4);

            btn2.put("sub_button",subBtns);

            buttons.add(btn2);
            json.put("button",buttons);


            String res = wxService.getMenuService().menuCreate(json.toJSONString());
            logger.info(" create default menu {}",res);
            return "执行结束";
        } catch (Exception e) {
            e.printStackTrace();
            return "出异常了呀~~~";
        }


    }


    @RequestMapping("/createApplyingMenu")
    public String createApplyingMenu(){

        try {
            JSONObject json = new JSONObject();

            JSONArray buttons = new JSONArray();
            JSONObject btn1 = new  JSONObject();
            btn1.put("name","我的申请");
            JSONArray applyBtns = new JSONArray();
            JSONObject applySub1 = new JSONObject();

            String keyStr = "{\"type\":\""+WxConsts.XML_MSG_TEXT+"\",\"content\":\"APPLYING\"}";
            applySub1.put("name","现在申请");
            applySub1.put("type","click");
            applySub1.put("key", keyStr);
            applyBtns.add(applySub1);

            JSONObject applySub2 = new JSONObject();
            applySub2.put("name","历史申请");
            applySub2.put("type","view");
            applySub2.put("url","http://www.baidu.com");
            applyBtns.add(applySub2);

            btn1.put("sub_button",applyBtns);

            buttons.add(btn1);


            JSONObject btn2 = new JSONObject();
            btn2.put("name","客户服务");

            JSONArray subBtns = new JSONArray();
            JSONObject sub1 = new JSONObject();
            sub1.put("name","关于我们");
            sub1.put("type","view");
            sub1.put("url","http://www.baidu.com");
            subBtns.add(sub1);

            JSONObject sub2 = new JSONObject();
            sub2.put("name","风险提示");
            sub2.put("type","view");
            sub2.put("url","http://www.baidu.com");
            subBtns.add(sub2);

            JSONObject sub3 = new JSONObject();
            sub3.put("name","逾期说明");
            sub3.put("type","view");
            sub3.put("url","http://www.baidu.com");
            subBtns.add(sub3);

            JSONObject sub4 = new JSONObject();
            sub4.put("name","如何还款");
            sub4.put("type","view");
            sub4.put("url","http://www.baidu.com");
            subBtns.add(sub4);

            btn2.put("sub_button",subBtns);

            buttons.add(btn2);
            json.put("button",buttons);

            JSONObject matchrole = new JSONObject();
            matchrole.put("tag_id", CustomerStatusEnum.APPLYING.getId());
            matchrole.put("language","zh_CN");

            json.put("matchrule",matchrole);

            String res = wxService.getMenuService().menuCreate(json.toJSONString());
            logger.info(" create applying menu {}",res);
            return "执行结束";
        } catch (Exception e) {
            e.printStackTrace();
            return "出异常了呀~~~";
        }


    }

    @RequestMapping("/test")
    public String test(){

        try{

//            wxService.getUserTagService().tagUpdate(CustomerStatusEnum.APPLYING.getId(),CustomerStatusEnum.APPLYING.getName());
//            wxService.getUserTagService().tagUpdate(CustomerStatusEnum.FINISHED.getId(),CustomerStatusEnum.FINISHED.getName());

//            wxService.getUserTagService().batchUntagging(100L,new String[]{"oPRymwkOvj7en6ioz6YhxmJ1InWU"});


            WxMpQrCodeTicket ticket = wxService.getQrcodeService().qrCodeCreateTmpTicket(1,60*5);
            logger.info("create tmp ticket {}",ticket);

        } catch (Exception e) {
            e.printStackTrace();
            return "出异常了拉~~~~";
        }




        return "啦啦啦啦啦~~~";
    }


}
